﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;

using Gift.Model;
using Gift.DAL;

public partial class admin_user_DepartmentList : BaseAdmin
{

    readonly DALDepartment theDepartment = new DALDepartment();
    tbDepartment oDepartment;
    // Methods
    private void bind()
    {
        if (Request.QueryString["action"] == "del")
        {
            theDepartment.Delete(int.Parse(Request.QueryString["uid"]));

            base.Response.Write("<script language='javascript'>;alert('删除成功');window.location.href='DepartmentList.aspx';</script>");
            base.Response.End();
        }


        DataTable dtSource = null;
        DataSet ds = theDepartment.GetList(string.Format(" and (DepName like '%{0}%' or DepCode='{0}' or DepManager like '{0}' or DepTel='{0}' or ManagerMobile='{0}')"
                                           , this.txtDepKeys.Text.Trim()));
        if (ds != null && ds.Tables.Count > 0)
        {
            dtSource = ds.Tables[0];
        }

        this.GridView1.DataSource = dtSource;
        this.GridView1.DataBind();

        this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
        this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
        this.LblRecordCount.Text = "总计 " + dtSource == null ? "0" : dtSource.Rows.Count.ToString() + "个记录";
        if (dtSource == null || dtSource.Rows.Count == 0)
        {
            this.btnFirst.Visible = false;
            this.btnPrev.Visible = false;
            this.btnNext.Visible = false;
            this.btnLast.Visible = false;
            this.LblCurrentIndex.Visible = false;
            this.LblPageCount.Visible = false;
            this.LblRecordCount.Visible = false;
        }
        else if (this.GridView1.PageCount == 1)
        {
            this.btnFirst.Visible = false;
            this.btnPrev.Visible = false;
            this.btnNext.Visible = false;
            this.btnLast.Visible = false;
        }
        this.btnFirst.CommandName = "1";
        this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
        this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
        this.btnLast.CommandName = this.GridView1.PageCount.ToString();
        this.ddlCurrentPage.Items.Clear();
        if (this.GridView1.PageCount > 0)
        {
            for (int i = 1; i <= this.GridView1.PageCount; i++)
            {
                this.ddlCurrentPage.Items.Add(i.ToString());
            }
            this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
        }
    }

    protected void PagerButtonClick(object sender, EventArgs e)
    {
        this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
        this.bind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
        this.bind();
    }





    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.bind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#efefef'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!base.IsPostBack)
        {
            base.ValidateLoginRule(Gift.Model.ENUM.ERule.DepartmentList);
            this.bind();
        }
    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        this.bind();
    }


}